#include<stdio.h>
#include<stdlib.h>
#include<string.h>

/*树的节点的定义*/
typedef struct HTNODE* ptr;
typedef struct HTNODE node;
struct HTNODE {
	int weight; //权值
	ptr l, r;
};
typedef ptr bstree;

/*数组*/
struct NODE {
	ptr data;
};
struct NODE num[70];

int size = 0; //堆的大小
int len = 0; //构造的Huffman树的大小（带权大小）

/*维护最小堆的性质*/
void minheapify(int i, int n)
{
	int l, r, min;
	while (1)
	{
		l = i << 1;
		r = i << 1 | 1;
		if (l <= n && num[l].data->weight < num[i].data->weight) min = l;
		else min = i;
		if (r <= n && num[r].data->weight < num[min].data->weight) min = r;
		if (min == i) return;
		struct NODE a = num[min]; //交换节点
			num[min] = num[i];
		num[i] = a;
		i = min;
	}
}

/*堆排序*/
void minheap()
{
	for (int i = size >> 1; i >= 1; i--)//从有叶节点父节点开始，到根节点，维护堆的性质
	{
		minheapify(i,size);
	}
}

/*堆的插入排序*/
void insert(bstree bt)
{
	size++;
	int i = size;
	for (; i / 2 > 0 && num[i / 2].data->weight > bt->weight; i /= 2)
	{
		num[i] = num[i / 2];
	}
	num[i].data = bt; //插入值的正确位置
}

/*从最小堆中取出最小元素*/
bstree deletmin() 
{
	bstree tep = num[1].data;
	num[1] = num[size--];
	minheapify(1, size);
	return tep;
}

/*每次取出最小的俩，组成新的节点，然后入堆*/
void huffman(int n)
{
		for (int i = 1; i < n; i++)
		{
			bstree t = (bstree)malloc(sizeof(node));
			t->l = deletmin();
			t->r = deletmin();
			t->weight = t->l->weight + t->r->weight;
			len += t->weight; //计算Huffman树的总权值
			insert(t);
		}
	//return deletmin();如果加上这个代码，返回的就是一棵完整的Huffman树的根节点的地址
}
int main(void)
{
	int n, m;
	scanf_s("%d", &n);
	getchar();
	size = n;
	int ff[50] = { 0 };
	for (int i = 1; i <= n; i++)
	{
		bstree p = (bstree)malloc(sizeof(node));
		char c;
		scanf_s(" %c%d", &c, &p->weight);
		getchar();
		ff[i] = p->weight;
		p->l = p->r = NULL;
		num[i].data = p;
	}
	minheap();
	huffman(n);
	scanf_s("%d", &m);
	getchar();
	char code[67][67];
	while (m--)
	{
		char c;
		for (int i = 1; i <= n; i++)
		{
			scanf_s("%c%s", &c, &code[i]);
			getchar();
		}
		int flag = 0;//初始定义为相同 
		for (int i = 1; i < n; i++)
		{
			for (int k = i + 1; k <= n; k++)
			{
				flag = 0;
				for (int j = 0; code[i][j] && code[k][j]; j++)
				{
					if (code[i][j] != code[k][j])
					{
							flag = 1;
							break;
					}
				}
					if (flag == 0) break;
			}
				if (flag == 0) break;
		}//里面是判断前缀编码是否重合，就是看看解码的时候是否唯一，然后我们在比较总权值的大小
			if (flag == 0)
				printf("No\n");//看是否前缀 
			else//无前缀，则看长度 
			{
				int tep = 0;
				for (int i = 1; i <= n; i++)
				{
					tep += ff[i] * strlen(code[i]);
				}
				if (tep != len) 
					printf("No\n");
				else 
					printf("Yes\n");
			}
	}
	//printf("%d\n",len);
	return 0;
}
